<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '500px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ daytime, side, noside } = {}) {
      this.chart.setOption({
        backgroundColor: '#fff',
        title: {
          text: '所有时间摆摊人员年副业情况',
          top: '10px',
          left: '10px',
          textStyle: {
            color: '#4887ec'
          }
        },

        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['副职业', '主职业'],
          top: '20px',
          textStyle: {
            color: '#000'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true

        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: daytime
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '副职业',
            type: 'line',
            stack: '总量',
            data: side
          },
          {
            name: '主职业',
            type: 'line',
            stack: '总量',
            data: noside
          }
        ]
      })
    }
  }
}
</script>
